package cn.pegasus.teamwork.appinterface.controller.meg;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigMessage;
import cn.pegasus.teamwork.sysconfig.service.SysConfigMessageService;

/**
 * @ClassName: MessageController
 * @Description: 消息中心
 * @author ryan.bian
 * @date 2017年12月23日 下午5:32:55
 * 
 * 
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/mgs/*")
@Controller
public class MessageController extends BaseController {

	@Autowired
	private SysConfigMessageService sysConfigMessageService;

	private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);

	/**
	 * 
	 * @param msgId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mg", method = RequestMethod.POST)
	public Map<String, Object> getMessageDetail(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id"},map);
		if (isNotNull) {
			int msgId = Integer.valueOf(map.get("id").toString());
			SysConfigMessage sysConfigMessage = new SysConfigMessage();
			sysConfigMessage.setId(msgId);
			sysConfigMessage = sysConfigMessageService.selectSingle(sysConfigMessage);
			if (sysConfigMessage != null) {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,sysConfigMessage);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} else {
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE,null);
		}
		return resultMap;
	}

	/**
	 * 根据用户上一次拉取系统消息的时间，来获取该时间之后的新消息条数
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mgc", method = RequestMethod.POST)
	public Map<String, Object> newMessageCount(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> tMap = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put("msgAcceptUserid", getSessionUserId());
		map.put("hasRead", 0);
		List<SysConfigMessage> msgList =sysConfigMessageService.query(map);
		try {
			if (msgList != null) {			
				tMap.put("count", msgList.size());
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,msgList.size());
			} else {
				tMap.put("count", 0);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,tMap);
			}
		} catch (Exception e) {
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}
		return resultMap;
	}
	
	/**
	 * 根据用户上一次拉取系统消息的时间，来获取该时间之后的新消息条数
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/wmgc", method = RequestMethod.POST)
	public Map<String, Object> newMessageWasherCount(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> tMap = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put("msgAcceptUserid", getSessionWasherId());
		map.put("hasRead", 0);
		List<SysConfigMessage> msgList =sysConfigMessageService.query(map);
		try {
			if (msgList != null) {			
				tMap.put("count", msgList.size());
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,msgList.size());
			} else {
				tMap.put("count", 0);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,tMap);
			}
		} catch (Exception e) {
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}
		return resultMap;
	}

	/**
	 * 获取消息列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mgs", method = RequestMethod.POST)
	public Map<String, Object> messageList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			SysConfigMessage sm = new SysConfigMessage();
			
			int userType = Integer.valueOf(map.get("userType").toString());
			if(userType ==1){
				sm.setMsgAcceptUserid(getSessionUserId());
			} else if(userType ==2){
				sm.setMsgAcceptUserid(getSessionWasherId());
			}
			
			sm.setUserType(userType);
			List<SysConfigMessage> messages = sysConfigMessageService.getSysConfigMessageList(sm);
			if (messages != null && messages.size() > 0) {
				Map<String, Object> mape = new HashMap<String, Object>();
            	mape.put("lis", messages);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("查询消息列表失败.", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}

		return resultMap;
	}
	
	/**
	 * 获取消息列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/wmgs", method = RequestMethod.POST)
	public Map<String, Object> messageWasherList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			SysConfigMessage sm = new SysConfigMessage();
			sm.setMsgAcceptUserid(getSessionWasherId());
			List<SysConfigMessage> messages = sysConfigMessageService.getSysConfigMessageList(sm);
			if (messages != null && messages.size() > 0) {
				Map<String, Object> mape = new HashMap<String, Object>();
            	mape.put("lis", messages);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("查询消息列表失败.", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}

		return resultMap;
	}

	/**
	 * 标志消息已读
	 * 
	 * @param map
	 *            消息id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uam", method = RequestMethod.POST)
	public Map<String, Object> updateAppealMsg(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (null != map.get("id")) {
				SysConfigMessage message = sysConfigMessageService.detail(map);
				// 0未读1已读
				message.setHasRead(1);
				message.setLastUpdateTime(new Date());
				sysConfigMessageService.update(message);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,true);
			}
		} catch (Exception e) {
			LOGGER.error("申诉信息回复信息失败！", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,false);
		}

		return resultMap;
	}
}
